﻿using PBO.Data.Util;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace PBO.Data.SQLite.DAL
{
    public class DALPokemonData : DALModel<PokemonData>
    {

        #region Instance

        private DALPokemonData() { }

        private static DALPokemonData instance;

        public static DALPokemonData GetInstance()
        {
            if (instance == null)
                instance = new DALPokemonData();
            return instance;
        }

        #endregion

        #region static

        public static DataTable GetAllNames()
        {
            string sql = "select pmid,(number||' '||name) name from pbo_pm";
            return DBUtility.ExecuteDataTable(sql);
        }

        #endregion

        #region counter

        public void CounterAdd(int pid)
        {
            DBUtility.ExecuteSql("update pbo_pm set UsageCount=UsageCount+1 where pmid=" + pid);
        }

        public void ClearCounter()
        {
            DBUtility.ExecuteSql("update pbo_pm set UsageCount=0");
        }

        public DataTable GetCounter()
        {
            return DBUtility.ExecuteDataTable("select Number,Forme,Name,UsageCount from pbo_pm where UsageCount > 0 order by UsageCount desc");
        }

        #endregion

        #region tool

        public List<PokemonData> GetByNumber(int number)
        {
            return base.GetModel("number=" + number);
        }

        public PokemonData GetModel(int number, int form)
        {
            return base.GetModel("number=" + number + " and Forme=" + form).FirstOrDefault();
        }

        public List<PokemonData> GetAllPokemon()
        {
            return base.GetModel();
        }

        public List<PokemonData> GetByType(params int[] types)
        {
            var arr = types.Where(i => i > 0).Distinct().ToArray();
            if (arr.Length == 1)
            {
                return base.GetModel("type2=0 and type1=" + arr[0]);
            }
            else if (arr.Length > 1)
            {
                return base.GetModel(string.Format("(type1={0} and type2={1}) or (type1={0} and type2={1})", arr[0], arr[1]));
            }
            return null;
        }

        #endregion

        #region override

        protected override string GetTableName()
        {
            return "pbo_pm";
        }

        protected override PokemonData DataRowToModel(DataRow dr)
        {
            var data = new PokemonData();

            data.Id = dr["PmId"].ToString().ToInt();
            data.Number = dr["Number"].ToString().ToInt();
            data.Forme = dr["Forme"].ToString().ToInt();
            data.NameBase = dr["Name"].ToString();
            data.Trait1 = (Trait)dr["Trait1"].ToString().ToInt();
            data.Trait2 = (Trait)dr["Trait2"].ToString().ToInt();
            data.EggGroup1 = (EggGroup)dr["EggGroup1"].ToString().ToInt();
            data.EggGroup2 = (EggGroup)dr["EggGroup2"].ToString().ToInt();
            data.GenderRestriction = (PokemonGenderRestriction)dr["GenderRestriction"].ToString().ToInt();
            data.Weight = dr["Weight"].ToString().ToDouble();
            data.BaseStats[StatType.Hp] = dr["HpBase"].ToString().ToInt();
            data.BaseStats[StatType.Atk] = dr["AtkBase"].ToString().ToInt();
            data.BaseStats[StatType.Def] = dr["DefBase"].ToString().ToInt();
            data.BaseStats[StatType.Speed] = dr["SpeedBase"].ToString().ToInt();
            data.BaseStats[StatType.SpAtk] = dr["SpAtkBase"].ToString().ToInt();
            data.BaseStats[StatType.SpDef] = dr["SpDefBase"].ToString().ToInt();
            data.Type1 = (BattleType)dr["Type1"].ToString().ToInt();
            data.Type2 = (BattleType)dr["Type2"].ToString().ToInt();
            data.BeforeEvolution = dr["BeforeEvolution"].ToString().ToInt();

            #region image
            data.FrontImage = dr["FrontImage"].ToString().ToLong();
            data.FrontImageF = dr["FrontImageF"].ToString().ToLong();
            data.BackImage = dr["BackImage"].ToString().ToLong();
            data.BackImageF = dr["BackImageF"].ToString().ToLong();
            data.Icon = dr["Icon"].ToString().ToLong();
            data.Frame = dr["Frame"].ToString().ToLong();
            data.FrameF = dr["FrameF"].ToString().ToLong();
            #endregion

            data.UsageCount = dr["UsageCount"].ToString().ToInt();
            return data;
        }

        public PokemonData GetModel(int id)
        {
            return base.GetModel("pmid=" + id).FirstOrDefault();
        }

        #endregion

    }
}
